package fr.azzale.casu.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import fr.azzale.casu.TribuneApplication;
import fr.azzale.casu.metier.Casu;
import fr.azzale.casu.metier.Line;

public class ParserXMLHandler extends DefaultHandler {

	private static final String TR = "tr";
	private static final String TD = "td";
	private static final String SPAN = "span";
	private static final String A = "a";
	private static final String IMG = "img";
	private boolean isTR;
	private boolean isTD;
	private boolean isSpan;
	private ArrayList<Line> lines;
	private Line currentLine;
	private boolean isA;
	private SimpleDateFormat df;
	private String currentA;
	private int currentColor;
	private Activity ctx;

	public ParserXMLHandler(Context ctx2) {
		this.ctx = (Activity) ctx2;
	}

	public ArrayList<Line> getData() {
		Collections.sort(lines, new Comparator<Line>() {

			@Override
			public int compare(Line object1, Line object2) {
				object1.getFormattedDate();
				object2.getFormattedDate();
				
				if(object1.getDate()==null || object2.getDate()==null)
					return 0;
				return -object1.getDate().compareTo(object2.getDate());
			}
		});
		return lines;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String str = new String(ch,start,length);
//		Log.d("parser", ""+str);
		if(isTR)
		{
			if(isTD)
			{
				if(isA && currentLine.hasOwner())
				{
					if(!str.startsWith("http://"))
					{
						currentLine.getOwner().setNom(str);
						
						currentLine.getOwner().setColor(currentColor);
					}
				}
				else if(isSpan && !currentLine.hasTime())
				{
					currentLine.setTime(str);
				}
				else if(isSpan)
				{
					if(!isA)
					{
		                currentLine.setText(currentLine.getText()+str); 
					}
				}
			}
		}
	}


	@Override
	public void startDocument() throws SAXException {
		lines = new ArrayList<Line>();
		//[15|Jul 05:39 pm]
		df = new SimpleDateFormat("DD MMM hh:mm a yyyy",Locale.US);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equals(TR))
		{
			if(currentLine != null)
			{
				if(currentLine.getOwner() == null)
				{
					currentLine.setOwner(new Casu());
				}
				lines.add(currentLine);
			}
			isTR = false;
		}
		else if(localName.equals(TD))
			isTD = false;
		else if(localName.equals(SPAN))
			isSpan = false;
		else if(localName.equals(A))
			isA = false;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(localName.equals(TR))
		{
			
			isTR = true;
			//shoutbox_row
			currentLine = new Line(Integer.parseInt(attributes.getValue("id").substring("shoutbox_row".length())));
		}
		else if(localName.equals(TD))
			isTD = true;
		else if(localName.equals(SPAN))
			isSpan = true;
		else if(localName.equals(A))
		{
			isA = true;
			currentA = "<a href="+attributes.getValue("href")+">LINK</a>";
			if(currentLine!=null && currentLine.getOwner()==null && attributes.getValue("href").startsWith(Utils.PROFILE_URL))
			{
				Casu c = ((TribuneApplication) ctx.getApplication()).getCasu(currentA);
				currentLine.setOwner(c);
				
			}
			else if(isSpan && currentLine!=null)
			{
	            currentLine.setText(currentLine.getText()+currentA); 
			}
		}
		else if(localName.equals(IMG))
		{
			currentLine.setText(currentLine.getText()+"<img src="+attributes.getValue("src")+"/>"); 
			
		}
		if(attributes!=null)
		{
			//style="color:#8b4513"
			if(attributes.getValue("style")!=null)
			{
				String attr = attributes.getValue("style");
				int sharpPosition = attr.indexOf("#");
				int semiColonPosition = attr.indexOf(";", sharpPosition);
				if(semiColonPosition==-1)
					semiColonPosition = attr.length();
				if(sharpPosition>=0)
				{
					currentColor = Color.parseColor(attr.substring(sharpPosition,semiColonPosition).toUpperCase());
				}
			}
		}
	}

}
